package com.dms.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.service.impl.BaseServiceImpl;
import com.dms.modules.system.entity.OperationLog;
import com.dms.modules.system.mapper.OperationLogMapper;
import com.dms.modules.system.service.OperationLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 操作日志服务实现类
 */
@Service
public class OperationLogServiceImpl extends BaseServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void logOperation(Long userId, String module, String operation, String content) {
        OperationLog log = new OperationLog();
        log.setUserId(userId);
        log.setModule(module);
        log.setOperation(operation);
        log.setContent(content);
        log.setCreateTime(LocalDateTime.now());
        // TODO: 设置IP和UserAgent
        save(log);
    }

    @Override
    protected void buildQueryWrapper(LambdaQueryWrapper<OperationLog> wrapper, Map<String, Object> conditions) {
        if (conditions.containsKey("userId")) {
            wrapper.eq(OperationLog::getUserId, conditions.get("userId"));
        }
        if (conditions.containsKey("module")) {
            wrapper.eq(OperationLog::getModule, conditions.get("module"));
        }
        if (conditions.containsKey("operation")) {
            wrapper.eq(OperationLog::getOperation, conditions.get("operation"));
        }
        if (conditions.containsKey("startTime")) {
            wrapper.ge(OperationLog::getCreateTime, conditions.get("startTime"));
        }
        if (conditions.containsKey("endTime")) {
            wrapper.le(OperationLog::getCreateTime, conditions.get("endTime"));
        }
    }
} 